package com.hk.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.annotation.AutoCheck;
import com.hk.common.Constant;
import com.hk.common.ResponseResult;
import com.hk.exception.ErrorCode;
import com.hk.exception.ThrowExceptionUtil;
import com.hk.model.entity.User;
import com.hk.model.vo.IdVO;
import com.hk.model.vo.user.*;
import com.hk.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author huangkun
 * @date 2024/12/11 21:51
 */
@RestController
//@Api(value = "用户管理", tags = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public ResponseResult register(@RequestBody UserRegisterVO registerDto) {
        Long id = userService.register(registerDto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public ResponseResult<UserVO> login(@RequestBody UserLoginVO loginVO, HttpServletRequest request) {
        return ResponseResult.success(userService.login(loginVO, request));
    }


    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/get/current")
    public ResponseResult<UserVO> getCurrentUser(HttpServletRequest request) {
        return ResponseResult.success(userService.getCurrentUser(request));
    }

    @ApiOperation(value = "退出")
    @GetMapping("/logout")
    public ResponseResult logout(HttpServletRequest request) {
        userService.logout(request);
        return ResponseResult.success();
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<UserVO> addUser(@RequestBody UserEditVO userVO) {
        return ResponseResult.success(userService.addUser(userVO));
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("/update")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<UserVO> updateUser(@RequestBody UserEditVO userVO) {
        return ResponseResult.success(userService.updateUser(userVO));
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/reset/password")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<Boolean> resetPassword(@RequestParam Long id) {
        User user = userService.getById(id);
        ThrowExceptionUtil.throwIf(user == null, ErrorCode.ERROR_PARAM, "用户不存在");
        String md5 = DigestUtil.md5Hex(Constant.USER_PASSWORD);
        User restUser = new User();
        restUser.setId(id);
        restUser.setPassword(md5);
        return ResponseResult.success(userService.updateById(restUser));
    }


    @ApiOperation(value = "根据id获取用户信息")
    @GetMapping("/get/id")
//    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<UserVO> getUserInfo(@RequestParam Long id) {
        return ResponseResult.success(userService.getUserInfo(id));
    }

    @ApiOperation(value = "获取用户列表")
    @PostMapping("/list")
    public ResponseResult<List<UserVO>> getUserList(@RequestBody UserQueryParamVO queryParamVO) {
        return ResponseResult.success(userService.getUserList(queryParamVO));
    }

    @ApiOperation(value = "获取用户分页列表")
    @PostMapping("/page")
    public ResponseResult<Page<UserVO>> getUserPageList(@RequestBody UserQueryParamVO queryParamVO) {
        if (queryParamVO.getCurrent() <= 0) {
            queryParamVO.setCurrent(1);
        }

        if (queryParamVO.getPageSize() <= 0) {
            queryParamVO.setPageSize(10);
        }
        if (queryParamVO.getPageSize() > 999) {
            queryParamVO.setPageSize(999);
        }
        return ResponseResult.success(userService.getUserPageList(queryParamVO));
    }


    @ApiOperation(value = "删除用户")
    @PostMapping("/delete")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult deleteUser(@RequestBody IdVO idVO) {
        userService.delete(idVO);
        return ResponseResult.success();
    }


}

